<?php
namespace SimplePhp\Database;

use SimplePhp\Database as database;
use SimplePhp\Map as map;

/**
 *
 */
class WhereConstraint implements IConstraint, IBindData {
	use database\TConstraintTableDependent;

	private $where;

	private $bindData;
	function __construct($Entiy) {
		$this->userSetWhere($Entiy);
		if (count($Entiy->getJoin()) > 0) {
			foreach ($Entiy->getJoin() as $key => $value) {
				$this->userSetWhere($value[0]);
			}
		}
	}

	/**
	 * 返回语句约束的类型
	 * @Overried
	 * @return [type] [description]
	 */
	public function getType() {
		return MysqlConstraintBuild::Where;
	}
	/**
	 * 获取这个约束组装完成的sql语句片段
	 * @return [type] [description]
	 */
	public function getSqlFragment() {
		if (!empty($this->where)) {
			$this->where = " where " . $this->where;
		}
		return $this->where;
	}

	/**
	 * 用户手动设置的查询条件
	 * @param  [type] $Entiy [数组方式的where的各种条件]
	 * @return [type]        [description]
	 */
	public function userSetWhere($Entiy, $group = 0) {
		$where = $Entiy->getWhere();
		$i = 0;
		foreach ($where as $key => $value) {
			if (is_array($value[0])) {
				$k = 0;
				foreach ($value as $key2 => $value2) {
					if (isset($value2['from'])) {
						$from = $value2['from'];
					} else {
						//取得默认的表名或者是表的别名
						$from = $this->getName($Entiy);
					}
					//这里设置表的默认where链接方式
					$contype = $k == count($value) - 1 ? "" : WhereCon::and_;

					$group = $k == count($value) - 1 ? 2 : 0;
					$group = $k == 0 ? 1 : $group;
					// $this->setWhere($from,$value2[0],$value2[1],isset($value2[2])?$value2[2]:database\Where::eq_,isset($value2[3])?$value2[3]:$contype,$group);
					$this->setWhere($from, $value2[0], ":" . $from . "_" . $value[0], isset($value2[2]) ? $value2[2] : database\Where::eq_, isset($value2[3]) ? $value2[3] : $contype, $group);
					$k++;
				}
				$i++;
			} else {
				if (isset($value['from'])) {
					$from = $value['from'];
				} else {
					//取得默认的表名或者是表的别名
					$from = $this->getName($Entiy);
				}
				//这里设置表的默认where链接方式
				$contype = $i == count($where) - 1 ? "" : WhereCon::and_;
				// $this->setWhere($from, $value[0], $value[1], isset($value[2]) ? $value[2] : database\Where::eq_, isset($value[3]) ? $value[3] : $contype, 0);
				$this->setWhere($from, $value[0], ":" . $from . "_" . $value[0], isset($value[2]) ? $value[2] : database\Where::eq_, isset($value[3]) ? $value[3] : $contype, 0);
				$i++;
			}
			$type = $this->getTablePropertyNode($Entiy, $value[0], new map\Field());
			$this->bindData[] = [":" . $from . "_" . $value[0], $value[1], map\Field::typeTrunPdoType($type->type)];
		}
	}

	/**
	 * 通过普通参数添加where
	 * @param [type] $from    [数据源]
	 * @param [type] $key     [数据库数据键]
	 * @param [type] $value   [数据]
	 * @param [type] $compare [比较方法]
	 * @param [type] $conType [连接方式]
	 */
	public function setWhere($from, $key, $value, $compare = database\Where::eq_, $conType = "", $group = 0) {
		if ($group == 0) {
			$group1 = "";
			$group2 = "";
		} else if ($group == 1) {
			$group1 = "(";
			$group2 = "";
		} else if ($group == 2) {
			$group1 = "";
			$group2 = ")";
		}
		$this->where .= " " . $group1 . $from . "." . $key . $compare . $value . $group2 . $conType;
	}

	public function getBindData() {
		return $this->bindData;
	}
}